package com.fd.wpx.authorizating.service.impl;

import com.fd.wpx.authorizating.service.SysRoleService;
import com.fd.wpx.blog.mapper.SysRoleMapper;
import com.fd.wpx.blog.pojo.SysRole;
import com.fd.wpx.blog.pojo.SysRoleExample;
import com.fd.wpx.common.pojo.BlogResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public BlogResult findRolebyId(Integer id) {
        BlogResult result=null;
        SysRole role=sysRoleMapper.selectByPrimaryKey(id);
        if(role!=null){
            result=BlogResult.ok(role);
        }else
        {
            result=BlogResult.build(404,"未找到");
        }

        return result;
    }

    @Override
    public BlogResult findRolebyIds(Integer[] ids) {
        BlogResult result=null;
        SysRoleExample example=new SysRoleExample();
        SysRoleExample.Criteria criteria=example.createCriteria();
        criteria.andRidIn(new ArrayList<Integer>(Arrays.asList(ids)));
        List<SysRole> roles=sysRoleMapper.selectByExample(example);
        if(roles.size()>0)
        {
            result=BlogResult.ok(roles);
        }else
        {
            result=BlogResult.build(404,"未找到数据");
        }
        return result;
    }

    @Override
    public BlogResult updateRole(SysRole role) {
        sysRoleMapper.updateByPrimaryKey(role);
        return BlogResult.ok();
    }

    @Override
    public BlogResult deleteRole(SysRole role) {
        sysRoleMapper.deleteByPrimaryKey(role.getRid());
        return BlogResult.ok();
    }

    @Override
    public BlogResult addRole(SysRole role) {
        sysRoleMapper.insert(role);
        return BlogResult.ok();
    }
}
